package com.cg.utils;

import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.concurrent.TimeUnit;

@Component
public class RedisUtil {

    @Resource
    private RedisTemplate<String,String> redisTemplate;

    //保存到redis中
    public void setValue(String key,String value,Integer timeOut){
        redisTemplate.opsForValue().set(key,value,timeOut, TimeUnit.SECONDS);
    }
    //获取
    public String get(String key){
        return redisTemplate.opsForValue().get(key);
    }

    //删除
    public void delete(String key){
        redisTemplate.delete(key);
    }

    //修改指定key的失效時間
    public void expire(String key,Long time){
        redisTemplate.expire(key,time,TimeUnit.SECONDS);
    }


    //根据key获取redis中保存的数据的失效时间
    public long getTime(String key) {
        return redisTemplate.getExpire(key);
    }
}
